313. 超级丑数
为保证权益,题目请参考 313. 超级丑数(From LeetCode).
解决方案1
Python
python
# 313. 超级丑数
# https://leetcode-cn.com/problems/super-ugly-number/
from typing import List
class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
choushu = [1]
pp = [0] * len(primes)
for i in range(1, n):
m = float("inf")
waitToAdd = []
for i in range(len(primes)):
t = primes[i] * choushu[pp[i]]
if m > t:
m = t
waitToAdd = [i]
elif m == t:
waitToAdd.append(i)
choushu.append(m)
for w in waitToAdd:
pp[w] += 1
return choushu[n - 1]
if __name__ == "__main__":
solution = Solution()
print(solution.nthSuperUglyNumber(1, [2, 3, 5]))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33